package com.yzf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzf.common.CustomException;
import com.yzf.entity.Category;
import com.yzf.entity.Dish;
import com.yzf.entity.Setmeal;
import com.yzf.mapper.CategoryMapper;
import com.yzf.mapper.DishMapper;
import com.yzf.mapper.EmployeeMapper;
import com.yzf.mapper.SetmealMapper;
import com.yzf.service.CategoryService;
import com.yzf.service.DishService;
import com.yzf.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yzf
 * @description
 * @since 2023/2/15 22:15
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(wrapper);
        if(count1>0){
            throw new CustomException("当前分类下关联了菜品,不能删除");
        }
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(queryWrapper);
        if(count2>0){
            if(count1>0){
                throw new CustomException("当前分类下关联了菜品,不能删除");
            }
        }
        super.removeById(id);
    }
}
